package br.com.ps.bombertournament.game.object;

import br.com.ps.bombertournament.game.BaseGameObject;
import br.com.ps.bombertournament.game.MappedGameObject;

public class Bomb extends MappedGameObject implements Cloneable {
	
	private static final float DEFAULT_LIFE_TIME = 3;
	private float currentLifeTime;
	private float endLifeTime;
	private int power;
	
	public Bomb(int power, float lifeTime) {
		endLifeTime = lifeTime;
		this.power = power;
	}
	
	public Bomb(int power) {
		this(power, DEFAULT_LIFE_TIME);
	}
	
	public Bomb() {
		this(1, DEFAULT_LIFE_TIME);
	}
	
	@Override
	public String getMapPath() {
		return "data/world/bomb/bomb.json";
	}
	
	@Override
	public void onRemove() {
		// Creating the fire bomb center
		{
			BombFire center = FactoryGameObject.createBombFireCenter();
			center.setPosition(super.getX(), super.getY());
			
			super.getLayer().add(center);
		}
		
		// Creating the fire bomb around
		{
			int relativePos = 1;
			for (int i = power; i > 0; i--) {
				
				{ // Creating up fire
					BombFire upExt = null;
					if (i == 1) {
						upExt = FactoryGameObject.createBombFireUpExt();
					} else {
						upExt = FactoryGameObject.createBombFireUp();
					}
					
					//upExt.setPosition(super.getX(), super.getY());
					upExt.setPosition(
							super.getX(), 
							super.getY() + (upExt.getHeight()  * relativePos * upExt.getScaleY()));
					
					super.getLayer().add(upExt);
				}
				
				{ // Creating down fire
					BombFire downExt = null;
					if (i == 1) {
						downExt = FactoryGameObject.createBombFireDownExt();
					} else {
						downExt = FactoryGameObject.createBombFireDown();
					}
					
//					downExt.setPosition(super.getX(), super.getY());
					downExt.setPosition(
							super.getX(), 
							super.getY() - (downExt.getHeight()  * relativePos * downExt.getScaleY()));
					
					super.getLayer().add(downExt);
				}
				
				{ // Creating left fire
					BombFire leftExt = null;
					if (i == 1) {
						leftExt = FactoryGameObject.createBombFireLeftExt();
					} else {
						leftExt = FactoryGameObject.createBombFireLeft();
					}
					
//					leftExt.setPosition(super.getX(), super.getY());
					leftExt.setPosition(
							super.getX() - (leftExt.getWidth()  * relativePos * leftExt.getScaleX()), 
							super.getY());
					
					super.getLayer().add(leftExt);
				}
				
				{ // Creating right fire
					BombFire rightExt = null;
					if (i == 1) {
						rightExt = FactoryGameObject.createBombFireRightExt();
					} else {
						rightExt = FactoryGameObject.createBombFireRight();
					}
					
//					rightExt.setPosition(super.getX(), super.getY());
					rightExt.setPosition(
							super.getX() + (rightExt.getWidth()  * relativePos * rightExt.getScaleX()), 
							super.getY());
					
					super.getLayer().add(rightExt);
				}
				
				relativePos++;
			}
		}
	}

	public void update(float deltaTime) {
		super.update(deltaTime);
		
		currentLifeTime += deltaTime;
		
		if (currentLifeTime > endLifeTime) {
			super.getLayer().remove(this);
		}
	}
	
	@Override
	public void onCollision(BaseGameObject other) {
		// Do nothing
	}

	public void setPower(int power) {
		this.power = power;
	}
}
